<?php 
class model_job extends Model
{
	
	function construct()
	{
		parent::__construct();
	}

	function getJobs($limit=20,$order = array("job.created", "desc"), $where = array())
	{
		$where['job.active']=1;
		$where['job.company_id'] = $_SESSION['user_company'];
		$this->db->select("job.*
							,user.firstname
							,user.lastname
							,company.company_name
							
							, lu_pri.lu_lable as job_pri
							, lu_type.lu_lable as job_type
							
							
							, lu_pri.lu_value as job_pri_id
							, lu_type.lu_value as job_type_id
							
							, timediff( job_end , job_start ) as spent2"
							
							
							)
					->from("job")
					->join("company", "job.company_id=company.id")
					->join("user", "user.id=job.job_logged_by")
					
					->join("lookup lu_type", "lu_type.lu_value=job.job_type and lu_type.lu_type='job_type'")
					
					->join("lookup lu_pri", "lu_pri.lu_value=job.job_priority and lu_pri.lu_type='job_pri'")

					
					
					
					->order_by($order[0], $order[1]);
					if($limit > 0)
					{
						$this->db->limit($limit);
					}
					
		
		if(!empty($where))
		{
			$this->db->where($where);
		}
		$r = $this->db->get();
//		echo $this->db->last_query();
		return $r->result_array();
	}	
	

//	function getJobs($limit=20,$order = array("created", "desc"))
//	{
//		$this->db->select("job.*,user.firstname,user.lastname,company.company_name, lu_pri.lu_lable as job_pri, lu_type.lu_lable as job_type")
//					->from("job")
//					->join("company", "job.company_id=company.id")
//					->join("user", "user.id=job.job_logged_by")
//					->join("lookup lu_type", "lu_type.lu_value=job_type and lu_type.lu_type='job_type'")
//					->join("lookup lu_pri", "lu_pri.lu_value=job_type and lu_pri.lu_type='job_pri'")
//					->order_by($order[0], $order[1])
//					->limit($limit);
//		$r = $this->db->get();
//		
//		return $r->result_array();
//	}
	
	function newJob($data)
	{
		$this->db->insert("job", $data);
	}
	
	function updateJob($data, $id)
	{
		$this->db->update("job", $data, array("id" => $id));
	}
	
	
	function updateJobStatus($jobId, $statusId)
	{
		$data = array("worklog_status" => $statusId);
		$this->db->where("job_id", $jobId);
		$this->db->update("worklog", $data);
		
	}
	
	function jobStart($jobId, $start_time="")
	{
		if($start_time == "")
		{
			$data = array("job_start" => date("Y-m-d  H i m s"));
		}
		else{
			$data = array("job_start" => $start_time);
		} 
		$this->updateJob($data, $jobId);
	}

	function jobEnd($jobId, $start_time="")
	{
		if($start_time == "")
		{
			$data = array("job_end" => date("Y-m-d  H i m s"));
		}
		else{
			$data = array("job_end" => $start_time);
		} 
		$this->updateJob($data, $jobId);
	}
	
	function getNextAutoIncrement(){
       $next_increment = 0;
       $query = $this->db->query("show table status like 'job'");
		foreach ($query->result() as $row) {
            $next_increment = $row->Auto_increment;
        }
        return $next_increment;
    }
    
    function invoiced($status,$id)
    {
    	$id = $this->input->post("id");
		$status = $this->input->post("status");		
		
		
		$updats = array("job_invoiced" => $status, "job_invoiced_by" => $_SESSION['user_id']);
		
		$this->updateJob($updats, $id);
    }


	function newNote()
	{
		$this->db->insert("notelog", $data);
	}
    
	function activate($id)
	{
		$data = array("active" =>1);
		$this->db->where("id", $id);
		$this->db->update("job", $data);
	}

	function deactivate($id)
	{
		$data = array("active" =>0);
		$this->db->where("id", $id);
		$this->db->update("job", $data);
	}
}


